using System.Web.Mvc;
using Cooking.Core.Infrastructure;
using Cooking.Core.Services;
using Cooking.Web.Code;
using Cooking.Web.ViewData;

namespace Cooking.Web.Controllers
{
    public class IngredientController : AppController
    {
        internal IIngredientService Service { get; set; }


        public IngredientController(IIngredientService service)
        {
            Service = service;
        }

        public ActionResult Index()
        {

            AddIngredientViewData vd = TempData["AddResult"] == null
                                           ? new AddIngredientViewData()
                                           : TempData["AddResult"] as AddIngredientViewData;
            return View("Add", vd);
        }

        public ActionResult Add(string description)
        {
            bool result = Service.CreateIngredient(description);
            AddIngredientViewData vd = new AddIngredientViewData {Added = result};

            if (result)
            {
                vd.Message = "Added ingredient: {0}".ToFormat(description);
                TempData["AddResult"] = vd;
                return RedirectToAction("Index");
            }
            else
            {
                vd.Message = "{0} already exists".ToFormat(description);
                vd.IngredientName = description;
                return View("Add", vd);
            }
        }
    }
}